﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyGiaoVuService.Domain
{
    public class TimeBoard
    {
        public List<XepLichItemObject> XepLichItemList { get; set; }
        public List<Guid> DanhSachMaLop { get; set; }

        public TimeBoard()
        {
            XepLichItemList = new List<XepLichItemObject>();
            DanhSachMaLop = new List<Guid>();
        }

        public TimeBoard(List<XepLichItemObject> itemList)
        {
            XepLichItemList = new List<XepLichItemObject>();
            foreach (XepLichItemObject obj in itemList)
                XepLichItemList.Add(new XepLichItemObject(obj));
            DanhSachMaLop = new List<Guid>();
            foreach (XepLichItemObject obj in XepLichItemList)
            {
                if (!DanhSachMaLop.Exists(e => e.Equals(obj.MaLopMonHoc)))
                {
                    Guid maLopMonHoc = obj.MaLopMonHoc;
                    DanhSachMaLop.Add(maLopMonHoc);
                }
            }
        }

        public List<Guid> LayDanhSachMaLopChuaXet()
        {
            var danhSachChuaXet = new List<Guid>(DanhSachMaLop);
            foreach (var item in XepLichItemList)
            {
                if (item.DuocChon == true)
                {
                    danhSachChuaXet.Remove(item.MaLopMonHoc);
                }
            }
            return danhSachChuaXet;
        }

        public TimeBoard Chon(XepLichItemObject obj)
        {
            TimeBoard timeBoard = new TimeBoard(XepLichItemList);
            foreach (var item in timeBoard.XepLichItemList)
            {
                if (item.MaLopMonHoc.Equals(obj.MaLopMonHoc))
                {
                    item.DuocChon = (item.ThuTu == obj.ThuTu);
                    item.DaXet = true;
                }
                if (TrungGio(item, obj))
                {
                    if (item.MaGiangVien.Equals(obj.MaGiangVien) || item.MaLopSinhVien.Equals(obj.MaLopSinhVien))
                        item.DaXet = true;
                }
            }
            return timeBoard;
        }

        public XepLichItemObject LayPhuongAnCaoNhatCuaLop(Guid maLop)
        {
            var dangKyTheoLop = XepLichItemList.Where(item => item.MaLopMonHoc.Equals(maLop));
            if (dangKyTheoLop.Count() > 0)
            {
                var dangKyTheoLopChuaXet = dangKyTheoLop.Where(item => item.DaXet == false);
                var itemObjectList = dangKyTheoLopChuaXet.Where(item => item.DoUuTien == dangKyTheoLopChuaXet.Min(dk => dk.DoUuTien));
                if (itemObjectList.Count() > 0)
                    return itemObjectList.First();
            }
            return null;
        }

        public void InitList()
        {
            foreach (XepLichItemObject obj in XepLichItemList)
            {
                    obj.DuocChon = false;
                    obj.DaXet = false;
                    if (!DanhSachMaLop.Exists(e => e.Equals(obj.MaLopMonHoc)))
                    {
                        Guid maLopMonHoc = obj.MaLopMonHoc;
                        DanhSachMaLop.Add(maLopMonHoc);
                    }
            }
        }

        //h = so cac lop mon hoc chua duoc xep lich
        public int GetH()
        {
            //int h = 0;
            //List<Guid> checkedMaLopMonHocList = new List<Guid>();
            //foreach (XepLichItemObject obj in XepLichItemList)
            //{
            //    if (!checkedMaLopMonHocList.Exists(e => e.Equals(obj.MaLopMonHoc)))
            //    {
            //        Guid maLopMonHoc = obj.MaLopMonHoc;
            //        checkedMaLopMonHocList.Add(maLopMonHoc);
            //        int index = XepLichItemList.FindIndex(item => item.MaLopMonHoc.Equals(maLopMonHoc) && item.DuocChon == true);
            //        if (index == -1)
            //            h++;
            //    }
            //}
            //return h;
            return LayDanhSachMaLopChuaXet().Count();
        }

        public int GetG()
        {
            int g = 0;
            foreach (XepLichItemObject obj in XepLichItemList)
            {
                if (obj.DuocChon == true)
                    g += obj.DoUuTien;
            }
            return g;
        }

        public int GetNextThuTu(int thuTuHienTai)
        {
            int next = 49;
            foreach (var item in XepLichItemList)
            {
                if (item.DaXet == false && item.ThuTu > thuTuHienTai && item.ThuTu < next)
                    next = item.ThuTu;
            }
            return next;
        }

        private bool TrungGio(XepLichItemObject obj1, XepLichItemObject obj2)
        {
            return ((obj1.Thu == obj2.Thu) && !(obj1.TietDau.GioBatDau >= obj2.TietSau.GioKetThuc || obj1.TietSau.GioKetThuc <= obj2.TietDau.GioBatDau));
        }
    }
}
